package com.beilang.hotline.dept.page;

import java.util.Date;
import java.util.List;

import com.beilang.base.core.page.BasePage;
import com.beilang.base.exception.CheckException;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.CodeConstant;
import com.beilang.hotline.dept.manager.DeptMgr;
import com.beilang.hotline.dept.model.DeptModel;
import com.beilang.hotline.dept.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;


public class DeptPage extends BasePage {
	private DeptMgr deptMgr;

	public void setDeptMgr(DeptMgr deptMgr) {
		this.deptMgr = deptMgr;
	}

	public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO) {
		helper = deptMgr.getDeptList(helper, searchTO);
		return helper;
	}

	public PaginationHelper deleteDeptList(PaginationHelper helper,SearchTO searchTO, List deleteList) {
        for (Object obj : deleteList) {
            DeptModel deptModel = (DeptModel) obj;
            deptMgr.deleteDept(deptModel.getDeptId());
        }
		return this.getSearchList(helper, searchTO);
	}

	public DeptModel addDept(DeptModel deptModel) throws CheckException {
		UserModel user=this.getLoginUser();
		List list=deptMgr.getAllDept();
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                DeptModel s = (DeptModel) obj;
                if (s.getDeptName().equalsIgnoreCase(deptModel.getDeptName())) {
                    throw new CheckException(AppDefaultProperty.getProperty(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST)));
                }
            }
		}
		deptModel.setCreateId(user.getUserId());
		deptModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		deptModel.setCreateTime(date);
		deptModel.setUpdateTime(date);
		deptModel = deptMgr.createDept(deptModel);
		return deptModel;
	}

	public DeptModel getViewDept(Long deptId) {
		return deptMgr.getDept(deptId);
	}

	public DeptModel getUpdateDept(Long deptId) {
		return this.getViewDept(deptId);
	}

	public DeptModel updateDept(DeptModel deptModel) throws CheckException{
		List list=deptMgr.getAllDept();
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                DeptModel s = (DeptModel) obj;
                if (s.getDeptName().equalsIgnoreCase(deptModel.getDeptName())) {
                    if (s.getDeptId().longValue() != deptModel.getDeptId().longValue())
                        throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
                }
            }
		}
		UserModel user=this.getLoginUser();
		deptModel.setUpdateId(user.getUserId());
		deptModel.setUpdateTime(DateUtil.getDate());
		deptModel = deptMgr.saveDept(deptModel);
		return deptModel;
	}

}
